<?php include('header.php');
$GLOBALS['option_value_count']		= 0;

?>


<style type="text/css">
	.sortable { list-style-type: none; margin: 0; padding: 0; width: 100%; }
	.sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; height: 18px; }
	.sortable li>span { position: absolute; margin-left: -1.3em; margin-top:.4em; }
</style>

<link type="text/css" href="<?php echo base_url('assets/css/upload.css');?>" rel="stylesheet" />

<script type="text/javascript" src="<?php echo base_url('assets/js/upload.js');?>"></script>
<script type="text/javascript">
//<![CDATA[

$(document).ready(function() {
	$(".sortable").sortable();
	$(".sortable > span").disableSelection();
	//if the image already exists (phpcheck) enable the selector

	<?php if($id) : ?>
	//options related
	var ct	= $('#option_list').children().size();
	// set initial count
	option_count = <?php echo count($product_options); ?>;
	<?php endif; ?>

});

//Upload Photo//
var fileTemplate="<div id=\"{{id}}\" >";
fileTemplate+="<div class=\"preview relative\"><div class=\"progressbar\"></div></div>";
fileTemplate+="</div>";
$html5Uploader={
	onClientLoadStart:function(e,file){
		var upload=$("#zonephoto");
		if(upload.is(":hidden")){
			upload.show();
		}
		upload.append(fileTemplate.replace(/{{id}}/g,slugify(file.name)).replace(/{{filename}}/g,file.name));
	},
	onClientLoad:function(e,file){
		$("#"+slugify(file.name)).find(".preview").append("<img src=\""+e.target.result+"\" alt=\"\">");
	},
	onServerLoadStart:function(e,file){
		$("#"+slugify(file.name)).find(".progressbar").progressbar({value:0});
	},
	onServerProgress:function(e,file){
	if(e.lengthComputable){
			var percentComplete=(e.loaded/e.total)*100;
			$("#"+slugify(file.name)).find(".progressbar").progressbar({value:percentComplete});
		}
	},
	onServerLoad:function(e,file){
		$("#"+slugify(file.name)).find(".progressbar").progressbar({value:100}).hide('slow');
	},
	onSuccess:function(e, file, responseText){
			$("#zonephoto").append(responseText);
		}
};

         
         
$(document).ready(function() {
	$html5Uploader.postUrl="<?php echo site_url($this->config->item('admin_folder').'/products/upload')?>";
	$html5Uploader.name=$("#Titre").val();
	$('#fileUpload').click(function() {
		$html5Uploader.name=$("#Titre").val();
		jQuery("#fileUpload").html5Uploader($html5Uploader);
	});
	jQuery("#zonephoto").html5Uploader($html5Uploader);
});



function remove_image(img)
{
	if(confirm("<?php echo lang('confirm_remove_image');?>"))
	{
		var id	= img.attr('rel')
		$(img).closest( ".span2" ).remove();
		$input='<input name="delPhoto[]" value="'+id+'" type="hidden"/>';
		$('#zonephoto').append($input);
	}
}


function remove_option(id)
{
	if(confirm("<?php echo lang('confirm_remove_option');?>"))
	{
		$('#option-'+id).remove();
	}
}

//]]>
</script>


<?php echo form_open($this->config->item('admin_folder').'/products/form/'.$id ); ?>
<div class="row-fluid">
	<div class="span10">
		<div class="tabbable">
			<ul class="nav nav-tabs">
				<li class="active"><a href="#product_info" data-toggle="tab"><?php echo lang('details');?></a></li>
				<li><a href="#product_categories" data-toggle="tab"><?php echo lang('categories');?></a></li>
				<li><a href="#product_options" data-toggle="tab"><?php echo lang('options');?></a></li>
				<li><a href="#product_seo" data-toggle="tab"><?php echo lang('header_information');?></a></li>
			</ul>
		</div>
		<div class="tab-content">
			<div class="tab-pane active" id="product_info">
				<div class="row-fluid">
						<?php
						$data	= array('placeholder'=>lang('name'), 'name'=>'name', 'value'=>set_value('name', $name), 'class'=>'span10','id'=>'Titre');
						echo form_input($data);
						?>
				</div>
				
				<div class="row-fluid">
						<?php
						$data	= array('name'=>'description','row'=>10, 'class'=>'redactor', 'value'=>set_value('description', $description));
						echo form_textarea($data);
						
						?>
				</div>
				<div class="row-fluid">
						<div id="gc_photos">
							
						<?php
						$data	= array('multiple'=>"multiple", 'id'=>"fileUpload", 'class'=>'span12','accept'=>"image/*" );
						echo form_upload($data);
						if(!empty($images))
						foreach($images as $image_id=>$link_photo)
						{	$checked=$class='';
							if($link_photo!='')
							{
								if(trim($link_photo)==trim($primary_photo)){
									$class=' class="primary"';
									$checked=' checked="checked"'; 
								}
								echo '
								<div class="span2">
										<div class="row">
											<img src="'.base_url('/images/thumbnails/'.$link_photo).'"  />
										</div>
										<div class="row">
											<input type="radio" name="primary_image" '.$checked.' value="'.trim($link_photo).'" /> '.lang('primary').'
											<a onclick="return remove_image($(this));" rel="'.$image_id.'" class="btn btn-danger"><i class="icon-trash icon-white"></i></a>
										</div>
								</div>';
							}

						}
						?>
						</div>
						
						<div id="zonephoto"></div>
				</div>
				
				<div class="row-fluid">
						<label><?php echo lang('excerpt');?></label>
						<?php
						$data	= array('name'=>'excerpt', 'value'=>set_value('excerpt', $excerpt), 'class'=>'span10', 'rows'=>5);
						echo form_textarea($data);
						?>
				</div>
				
			</div>
						
			<div class="tab-pane" id="product_categories">
				<div class="row-fluid">
						<label><strong><?php echo lang('select_a_category');?></strong></label>
				</div>
				<div class="row-fluid">
					<div class="span4">
						<select id="category_list" size="5">
							<?php foreach ($category_list as $cat) 
								echo "<option id='category_item_$cat->id' value='$cat->id'>$cat->name</option>";
							?>
						</select>
						
						<input  type="text" id="category_search" />
						<script type="text/javascript">
						$('#category_search').keyup(function(){
							$('#category_list').html('');
							run_category_query();
						});

						function run_category_query()
						{
							$.post("<?php echo site_url($this->config->item('admin_folder').'/categories/category_autocomplete/');?>", { name: $('#category_search').val(), limit:10},
								function(data) {

									$('#category_list').html('');

									$.each(data, function(index, value){

										if($('#category_'+index).length == 0)
										{
											$('#category_list').append('<option id="category_item_'+index+'" value="'+index+'">'+value+'</option>');
										}
									});

							}, 'json');
						}
						</script>
						<p>
							<a href="#" onclick="add_category(); return false;" class="btn" title="Add Category"><?php echo lang('add_category');?></a>
						</p>	
					</div>
					<div class="span8">
						<table class="table table-striped" style="margin-top:10px;">
							<tbody id="categories_container">
							<?php							
							foreach($product_categories as $cat)
							{
								echo category($cat->id, $cat->name);
							}
							?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
			
			<div class="tab-pane" id="product_seo">
				<div class="row-fluid">
					<div class="span10">
						
						<label for="slug"><?php echo lang('slug');?> </label>
						<?php
						$data	= array('name'=>'slug', 'value'=>set_value('slug', $slug), 'class'=>'span10');
						echo form_input($data);?>
						
						<label for="seo_title"><?php echo lang('seo_title');?> </label>
						<?php
						$data	= array('name'=>'seo_title', 'value'=>set_value('seo_title', $seo_title), 'class'=>'span10');
						echo form_input($data);
						?>

						<label for="meta"><?php echo lang('meta');?> <i><?php echo lang('meta_example');?></i></label> 
						<?php
						$data	= array('name'=>'meta', 'value'=>set_value('meta', html_entity_decode($meta)), 'class'=>'span10');
						echo form_textarea($data);
						?>
					</div>
				</div>
			</div>
			<div class="tab-pane" id="product_options">
				<div class="row-fluid">
						<div class="pull-right" style="padding:0px 0px 10px 0px;">
							<input id="add_option" class="btn" type="button" onclick="add_option_value()" value="<?php echo lang('add_option');?>" style="margin:0px;"/>
						</div>
				</div>
				<div id="option-items" class="option-items">
					<?php 
					if(!empty($product_options)){
						foreach($product_options as $po)
						{
							echo '<div class="row-fluid option-values-form">
									<div class="span1"><a class="handle btn btn-mini""><i class="icon-align-justify"></i></a></div>
									<div class="span6">A partir de <input class="span2" value="'.$po->unit.'" name="option['.$po->id.'][unit]" type="text"/> unités : 
									<input class="span2" name="option['.$po->id.'][price]" value="'.$po->price.'" type="text"> € l\'unité</div>
									<div class="span2"><a class="delete-option-value btn btn-danger btn-mini pull-right"><i class="icon-trash icon-white"></i></a></div>
								</div>';
						}
					}
					?>
					
				</div>
				<script type="text/javascript">
				$('.delete-option-value').click(function(){
					$(this).closest('.option-values-form').remove();
				});
				
				function add_option_value()
				{
					count++;
					htmlOption=
						'<div class="row-fluid option-values-form">'+
							'<div class="span1"><a class="handle btn btn-mini""><i class="icon-align-justify"></i></a></div>'+
							'<div class="span6">A partir de <input class="span2" value="" name="option['+count+'][unit]" type="text"/> unités : '+
							'<input class="span2" name="option['+count+'][price]" value="" type="text"> € l\'unité</div>'+
							'<div class="span2"><a class="delete-option-value btn btn-danger btn-mini pull-right"><i class="icon-trash icon-white"></i></a></div>'+
						'</div>';
					$('#option-items').append(htmlOption);
				}
				
				</script>
			</div>

			<div class="tab-pane" id="product_related">
				<div class="row-fluid">
						<label><strong><?php echo lang('select_a_product');?></strong></label>
				</div>
				<div class="row-fluid">
					<div class="span4">
								<input type="text" id="product_search" />
								<script type="text/javascript">
								$('#product_search').keyup(function(){
									$('#product_list').html('');
									run_product_query();
								});
						
								function run_product_query()
								{
									$.post("<?php echo site_url($this->config->item('admin_folder').'/products/product_autocomplete/');?>", { name: $('#product_search').val(), limit:20},
										function(data) {
									
											$('#product_list').html('');
									
											$.each(data, function(index, value){
									
												if($('#related_product_'+index).length == 0)
												{
													$('#product_list').append('<option id="product_item_'+index+'" value="'+index+'">'+value+'</option>');
												}
											});
									
									}, 'json');
								}
								</script>
								<select id="product_list"  class="row-fluid" size="10" style="margin:0px;"></select>
								<a href="#" onclick="add_related_product();return false;" class="btn" title="Add Related Product"><?php echo lang('add_related_product');?></a>
							
					</div>
					<div class="span8">
						<table class="table table-striped" style="margin-top:10px;">
							<tbody id="product_items_container">
							<?php
							foreach($related_products as $rel)
							{
								echo related_items($rel->id, $rel->name);
							}
							?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
			
		</div>
	</div>
	<div class="span2">
	
		<label for="promotion"><?php echo lang('promotion');?> </label>
		<?php
		$data	= array('name'=>'promotion', 'value'=>1,  'checked'     => $promotion);
		echo form_checkbox($data);?>
		
		<label for="price"><?php echo lang('price');?></label>
		<?php
		$data	= array('name'=>'price', 'value'=>set_value('price', $price));
		echo form_input($data);?>
		
		<label for="saleprice"><?php echo lang('saleprice');?></label>
		<?php
		$data	= array('name'=>'saleprice', 'value'=>set_value('saleprice', $saleprice));
		echo form_input($data);?>
		<label for="quantity"><?php echo lang('quantity');?> </label>
		<?php
		$data	= array('name'=>'quantity', 'value'=>set_value('quantity', $quantity));
		echo form_input($data);
		?>
		<?php
		$data	= array('enabled'=>'1');
		echo form_hidden($data);
		/*
		$data	= array('taxable'=>'1');
		echo form_hidden($data);
		$data	= array('shippable'=>'1');
		echo form_hidden($data);
		*/
		?>
	</div>
</div>

<div class="form-actions">
	<button type="submit" class="btn btn-primary"><?php echo lang('form_save');?></button>
</div>
</form>

<?php
//this makes it easy to use the same code for initial generation of the form as well as javascript additions
function replace_newline($string) {
  return trim((string)str_replace(array("\r", "\r\n", "\n", "\t"), ' ', $string));
}
?>
<script type="text/javascript">
//<![CDATA[
var option_count		= 0;
var count	= 0;

function add_related_product()
{
	//if the related product is not already a related product, add it
	if($('#related_product_'+$('#product_list').val()).length == 0 && $('#product_list').val() != null)
	{
		<?php $new_item	 = str_replace(array("\n", "\t", "\r"),'',related_items("'+$('#product_list').val()+'", "'+$('#product_item_'+$('#product_list').val()).html()+'"));?>
		var related_product = '<?php echo $new_item;?>';
		$('#product_items_container').append(related_product);
		run_product_query();
	}
	else
	{
		if($('#product_list').val() == null)
		{
			alert("<?php echo lang('alert_select_product');?>");
		}
		else
		{
			alert("<?php echo lang('alert_product_related');?>");
		}
	}
}

function add_category()
{
	//if the related product is not already a related product, add it
	if($('#categories_'+$('#category_list').val()).length == 0 && $('#category_list').val() != null)
	{
		<?php $new_item	 = str_replace(array("\n", "\t", "\r"),'',category("'+$('#category_list').val()+'", "'+$('#category_item_'+$('#category_list').val()).html()+'"));?>
		var category = '<?php echo $new_item;?>';
		$('#categories_container').append(category);
		run_category_query();
	}
}


function remove_related_product(id)
{
	if(confirm("<?php echo lang('confirm_remove_related');?>"))
	{
		$('#related_product_'+id).remove();
		run_product_query();
	}
}

function remove_category(id)
{
	if(confirm("<?php echo lang('confirm_remove_category');?>"))
	{
		$('#category_'+id).remove();
		run_product_query();
	}
}

//]]>
</script>
<?php
function related_items($id, $name) {
	return '
			<tr id="related_product_'.$id.'">
				<td>
					<input type="hidden" name="related_products[]" value="'.$id.'"/>
					'.$name.'</td>
				<td>
					<a class="btn btn-danger pull-right btn-mini" href="#" onclick="remove_related_product('.$id.'); return false;"><i class="icon-trash icon-white"></i> '.lang('remove').'</a>
				</td>
			</tr>
		';
}

function category($id, $name) {
	return '
			<tr id="category_'.$id.'">
				<td>
					<input type="hidden" name="categories[]" value="'.$id.'"/>
					'.$name.'</td>
				<td>
					<a class="btn btn-danger pull-right btn-mini" href="#" onclick="remove_category('.$id.'); return false;"><i class="icon-trash icon-white"></i> '.lang('remove').'</a>
				</td>
			</tr>
		';
}

include('footer.php'); ?>